package com.yan.deezer.ui.adapter;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.yan.deezer.R;
import com.yan.deezer.data.manager.DataManager;
import com.yan.deezer.data.object.Track;
import com.yan.deezer.utils.SingletonMediaPlayer;

public class AlbumTrackListAdapter extends ArrayAdapter<Track> implements
		OnItemClickListener {
	private static final String TAG = "YAN_DEEZER_ALBUM'S_TRACK_LIST_ADAPTER";

	protected LayoutInflater mInflater;
	protected List<Track> mList;
	protected int mnViewResId;
	protected Track currentTrack;
	protected DataManager mDataManager;
	protected Context mContext;
	protected TrackViewHolder holder;
	protected TextView textStreamed;

	public AlbumTrackListAdapter(Context context, List<Track> list, int resId) {
		super(context, resId, list);
		mInflater = LayoutInflater.from(context);
		mList = list;
		mnViewResId = resId;
		mDataManager = DataManager.getInstance();
		mContext = context;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mList != null) {
			size = mList.size();
		}
		return size;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	static class TrackViewHolder {

		/*
		 * 
		 * 
		 * { "id": "27", "name": "Daft Punk", "link":
		 * "http://www.deezer.com/music/daft-punk", "picture":
		 * "http://api.deezer.com/2.0/artist/27/image", "nb_album": "48",
		 * "nb_fan": "271184", "radio": true, "type": "artist" }
		 */

		ImageButton trackPlayStopKey;
		TextView trackName;
		boolean isPlaying;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		holder = null;

		if (convertView == null) {
			convertView = mInflater.inflate(mnViewResId, null);
			holder = new TrackViewHolder();
			/*
			 * holder.trackPlayStopKey = (ImageButton) convertView
			 * .findViewById(R.id.track_cover);
			 */
			holder.trackName = (TextView) convertView
					.findViewById(R.id.track_title);

			convertView.setTag(holder);
		} else {
			holder = (TrackViewHolder) convertView.getTag();
		}

		// On associe les donnes avec le holder correspondant
		currentTrack = getItem(position);

		// holder.articleTrailerClickZone.setTag(mCurrentarticle.getTrailer());

		int id = Integer.parseInt(currentTrack.getId());

		holder.trackName.setText(currentTrack.getTitle());
		holder.trackName.setSelected(true);
		holder.trackName.setCompoundDrawablePadding(3);

		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		Track track = getItem(position);

		if (track != null) {

			if (track.getPreview().isEmpty()
					|| track.getReadable().contains("false")) {
				Toast.makeText(mContext, "Cette piste ne se joue pas",
						Toast.LENGTH_SHORT);
				return;
			}

			if (!track.getPreview().isEmpty()) {

				// playStopEvent(holder.trackPlayStopKey,
				// currentTrack.getPreview());

				try {
					SingletonMediaPlayer.getInstance().startStreaming(mContext,
							track.getPreview());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Toast.makeText(mContext, R.string.track_list_title,
							Toast.LENGTH_SHORT);
				}

			}

		}

	}

}
